package so.shanku.essential.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import aym.util.json.JsonMap;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;
import so.shanku.essential.utils.StringUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 16/2/29.
 */
public class LinkProductAdapter extends MyBaseAdapter {
    public LinkProductAdapter(Context context) {
        super(context);
    }

    public LinkProductAdapter(Context context, List<? extends Map<String, ?>> datas) {
        super(context, datas);
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LinkProductAdapterViewHolder linkProductAdapterViewHolder;
        if (view == null) {
            view = layoutInflater.inflate(R.layout.link_relation_goods_item, null);
            linkProductAdapterViewHolder = new LinkProductAdapterViewHolder();
            ViewUtils.inject(linkProductAdapterViewHolder, view);
            view.setTag(linkProductAdapterViewHolder);
        } else {
            linkProductAdapterViewHolder = (LinkProductAdapterViewHolder) view.getTag();
        }
        bindData(i, linkProductAdapterViewHolder);
        return view;
    }


    private void bindData(final int i, LinkProductAdapterViewHolder viewHolder) {
        JsonMap jsonMap= (JsonMap) datas.get(i);

        String picPath=jsonMap.getStringNoNull("ProductPic");
        String scalePic= StringUtil.get_MImage(picPath);
        if(MyApplication.getInstance().getBitmapHashMap().containsKey(scalePic)) {
            viewHolder.goods_logo.setImageBitmap(MyApplication.getInstance().getBitmapHashMap().get(scalePic));
        } else {
            ImageLoader.getInstance().displayImage(scalePic,viewHolder.goods_logo,MyApplication.getInstance().getDefaultOption(),new ImageLoaderBitmapCallBack());
        }

        viewHolder.goods_discribe_tv.setText(jsonMap.getStringNoNull("SalesName"));
        viewHolder.goods_price_tv.setText(StringUtil.getFormatMoneyWithSign(jsonMap.getStringNoNull("Price")));
        viewHolder.goods_source_tv.setText(""+jsonMap.getStringNoNull("brandName"));
    }


    public class LinkProductAdapterViewHolder {
        @ViewInject(R.id.goods_logo)
        public ImageView goods_logo;

        @ViewInject(R.id.goods_discribe_tv)
        public TextView goods_discribe_tv;

        @ViewInject(R.id.goods_source_tv)
        public TextView goods_source_tv;

        @ViewInject(R.id.goods_price_tv)
        public TextView goods_price_tv;
    }

}
